<?php 
/**
 * Author: Zheng_Great
 * Description:  YII2 sql查询练习 小案例
 * Date: 2019-03-05 17:59:39
 */

namespace frontend\controllers;
use yii;
use yii\web\Controller;
use frontend\models\artide\SqlPractice;


class SqlcombatController extends Controller{

	public function actionIndex(){
		$sqlModel=new SqlPractice();
		$result=$sqlModel->find()->asarray()->all();

		return $this->render('index',['result'=>$result]);
	}


	#查询数据
	public function actionView($id){
		// echo $id;
		$result=SqlPractice::find()->where('id = :id',[':id'=>$id])->asarray()->one();
		// print_r($result);
		return $this->render('view',['result'=>$result]);
	}

	#增加数据
	public function actionCreate(){

		if(Yii::$app->request->isPost){
			$SqlModel=new SqlPractice();
			
			$repost=Yii::$app->request->post();
			$SqlModel->imgurl= $repost['imgurl'];
			$SqlModel->status= $repost['status'];
			$SqlModel->flag= $repost['flag'];

			if($SqlModel->save()){
				return $this->redirect('index');
			}else{
				echo '抱歉数据删除失败！';
			}

			return false;
		}
		return $this->render('create');
	}

	#删除数据
	public function actionDel($id){
		$res=SqlPractice::deleteAll('id =:uid',['uid'=>$id]);
		if($res){
			echo '删除成功';
			return $this->redirect('index');
		}
		echo '删除失败';
	}

	#更新数据
	public function actionUpdate($id){
		// $sqlModel=new SqlPractice();
		// $sqlModel=$sqlModel->find()->where('id=:id',[':id'=>$id])->asarray()->one();

		$sqlModel=SqlPractice::find()->where('id=:id',[':id'=>$id])->one();
		// print_r($sqlModel->imgurl);
		// exit;

		if(Yii::$app->request->isPost){
			$post=Yii::$app->request->post();

			if($sqlModel->changeDate($post)){
				return $this->redirect('index');			
			}else{
				echo '抱歉数据更新失败！';
			}

		}
		return $this->render('update',['sqlmodel'=>$sqlModel]);	
	}


}



//该做一个展示页面，
//在这个页面有增，删，改，查，的功能
 ?>